3. Implementing Changes to the Controller Class
Let's return to DudelViewController, and make the implementation changes to match the new things in the interface. Open DudelViewController.m, and start off by adding the following near the top of the file, so that the controller will get access to the new TextTool class we'll soon create:
#import "TextTool.h"
We also have the new font property to synthesize. Add it to the existing line:
@synthesize currentTool, fillColor, strokeColor, strokeWidth, font;
Next, we want to make sure we have a default value for the font, so add a line to viewDidLoad, like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.currentTool = [DotTool sharedDotTool];
[dotButton setImage:[UIImage imageNamed:@"button_dot_selected.png"]];
self.fillColor = [UIColor colorWithWhite:0.0 alpha:0.25];
self.strokeColor = [UIColor blackColor];
self.font = [UIFont systemFontOfSize:24.0];
}
Now let's update the deselectAllToolButtons method so that it knows about the new button:
- (void)deselectAllToolButtons {
[textButton setImage:[UIImage imageNamed:@"button_text.png"]];
[freehandButton setImage:[UIImage imageNamed:@"button_bezier.png"]];
[ellipseButton setImage:[UIImage imageNamed:@"button_ellipse.png"]];
[rectangleButton setImage:[UIImage imageNamed:@"button_rectangle.png"]];
[lineButton setImage:[UIImage imageNamed:@"button_line.png"]];
[dotButton setImage:[UIImage imageNamed:@"button_dot.png"]];
}
Finally, implement the method that the new button will call:
- (IBAction)touchTextItem:(id)sender {
self.currentTool = [TextTool sharedTextTool];
[self deselectAllToolButtons];
[textButton setImage:[UIImage imageNamed:@"button_text_selected.png"]];
}
Those are all the changes our controller class needs in order to handle the new tool. Next, let's update the ToolDelegate
protocol to match the new bit of functionality in the controller. Our
new Text tool will want to get the currently selected font from the
controller, which it knows of only as an object conforming to the ToolDelegate protocol. Edit the Tool.h file, and add a line to the ToolDelegate section at the bottom:
@protocol ToolDelegate
- (void)addDrawable:(id <Drawable>)d;
- (UIView *)viewForUseWithTool:(id <Tool>)t;
- (UIColor *)strokeColor;
- (UIColor *)fillColor;
- (UIFont *)font;
@end